//
//  MineViewController.m
//  FreeReading
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MineViewController.h"
#import "MineCell.h"
#import "NightCell.h"
#import "AboutUs.h"
#import "CollectionController.h"

@interface MineViewController () <UITableViewDataSource, UITableViewDelegate>
{
    BOOL _flag;
}
@property (nonatomic, strong) UITableView *tableView;

@end
@implementation MineViewController
-(void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NIGHT" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    _flag = NO;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineCell" bundle:nil] forCellReuseIdentifier:@"MineCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NightCell" bundle:nil] forCellReuseIdentifier:@"NightCell"];
    
    [self addSubviews];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nightAndDay:) name:@"NIGHT" object:nil];
    
}
#pragma mark - 通知执行方法
- (void)nightAndDay:(NSNotification *)notification
{
    if ([notification.userInfo[@"state"] isEqual:@1]) {
        _flag = YES;
        // 夜间模式
        [[NSUserDefaults standardUserDefaults] setObject:kNight forKey:KEK];
        [[UIApplication sharedApplication].delegate window].alpha = 0.5;
        [self.tableView reloadData];

    } else {
        _flag = NO;
        // 白天模式
        [[NSUserDefaults standardUserDefaults] setObject:kDay forKey:KEK];
        [[UIApplication sharedApplication].delegate window].alpha = 1.0;
        [self.tableView reloadData];

    }
}

- (void)addSubviews
{
    self.tableView.bounces = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    headerView.backgroundColor = [UIColor redColor];
    headerView.image = [UIImage imageNamed:@"8"];
    self.tableView.tableHeaderView = headerView;

    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 80) / 2, (headerView.height - 80) / 2, 80, 80)];
    image.layer.cornerRadius = 40;
    image.layer.masksToBounds = YES;
    image.image = [UIImage imageNamed:@"icon@2x"];
    [headerView addSubview:image];
    image.backgroundColor = [UIColor redColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 100) / 2, image.maxY + kMargin / 4, 100, 30)];
    titleLabel.text = @"FR";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:titleLabel];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.hidesBottomBarWhenPushed = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && self.view.window == nil) {
        
        self.view = nil;
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
             MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell" forIndexPath:indexPath];
            cell.icon.image = [UIImage imageNamed:@"iconfont-shoucang01"];
            cell.titleLabel.text = @"我的收藏";
            
            
        
            
            return cell;
            
        } else if (indexPath.row == 1) {
            
            NightCell *nightCell = [tableView dequeueReusableCellWithIdentifier:@"NightCell" forIndexPath:indexPath];
            if (_flag == NO) {
            nightCell.icon.image = [UIImage imageNamed:@"iconfont-baitianmoshi"];
            } else if (_flag == YES) {
            nightCell.icon.image = [UIImage imageNamed:@"iconfont-yejianzhuanhuan"];
            }
            nightCell.titleLabel.text = @"夜间模式";
            return nightCell;

        } else {
             MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell" forIndexPath:indexPath];
            
            cell.icon.image = [UIImage imageNamed:@"iconfont-qinglihuancun"];
            cell.titleLabel.text = @"清理缓存";
            return cell;

        }
        
    } else {
        
        if (indexPath.row == 0) {
             MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell" forIndexPath:indexPath];
            cell.icon.image = [UIImage imageNamed:@"iconfont-copyright"];
            cell.titleLabel.text = @"免责声明";
            return cell;

        } else if (indexPath.row == 1) {
             MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell" forIndexPath:indexPath];
            cell.icon.image = [UIImage imageNamed:@"iconfont-banbenxinxi"];
            cell.titleLabel.text = @"版本信息";
            return cell;

        } else {
             MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell" forIndexPath:indexPath];
            cell.icon.image = [UIImage imageNamed:@"iconfont-guanyuwomen"];
            cell.titleLabel.text = @"关于我们";
            return cell;

        }
        
    }
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
#pragma mark - Cell 点击方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
        
            CollectionController *collctVC = [[CollectionController alloc] init];
            
            [self.navigationController pushViewController:collctVC animated:YES];
            
            
        } else if (indexPath.row == 1) {
            return;
            
        } else {
            
         CGFloat size = [[EnjoyDataManager shareManager] getCachesSize];
            NSString *message = size > 1 ? [NSString stringWithFormat:@"缓存大小%0.2fM 清除缓存?", size] : [NSString stringWithFormat:@"缓存大小%0.2fK 清除缓存?", size * 1024.0];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                [[EnjoyDataManager shareManager] cleanCachesInView:self.view];
                [alert dismissViewControllerAnimated:YES completion:nil];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                [alert dismissViewControllerAnimated:YES completion:nil];
                return ;
                
            }];

            [alert addAction:action];
            [alert addAction:cancel];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        
    } else {
        
        if (indexPath.row == 0) {
    
            NSString *message = @"本app所有内容，包括文字、图片、音频、视频、软件、程序、以及版式设计等均在网上搜集。访问者可将本app提供的内容或服务用于个人学习、研究或欣赏，以及其他非商业性或非盈利性用途，但同时应遵守著作权法及其他相关法律的规定，不得侵犯本app及相关权利人的合法权利。除此以外，将本app任何内容或服务用于其他用途时，须征得本app及相关权利人的书面许可，并支付报酬。本app内容原作者如不愿意在本app刊登内容，请及时通知本app，予以删除。\n地址：\n电话：\n传真：\n电子邮箱：";
            
            [self showAlertControllerWithTitle:@"版权声明" message:message];
            
        } else if (indexPath.row == 1) {
           
            
        [self showAlertControllerWithTitle:@"版本信息" message:@"FR version 1.0 最新版本"];
            
        } else {
            
            AboutUs *aboutVc = [[AboutUs alloc] init];
            [self.navigationController pushViewController:aboutVc animated:YES];
    }
    
    }
}

// 提示框方法
- (void)showAlertControllerWithTitle:(NSString *)title message:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
        return ;
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];

}
@end
